package server.main.pojo;

import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
//import server.main.service.AuthAccountGroupsService;
//import server.main.service.AuthAccountPermissionsService;
//import server.main.util.SpringBeanUtils;

import javax.print.Doc;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.*;


@Data
@TableName(value = "server_account")
public class Account implements  UserDetails {
    private static final PasswordEncoder ENCODER = new BCryptPasswordEncoder();

    /**
     * 数据库字段
     */
    @TableId(type = IdType.AUTO)
    private Integer id;

    private String username;

    private String password;


    @TableField(value = "doctor_jobnum")
    private String jobnum;

    private String personalization;

    @TableField(value = "gmt_modified", fill = FieldFill.UPDATE)
    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
    @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")
    private Date gmtModified;

    @TableField(value = "gmt_create",fill = FieldFill.INSERT)
    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
    @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")
    private Date gmtCreate;

    @Version
    @TableField(fill = FieldFill.INSERT)
    private Integer version;


    @TableField(value = "isenable")
    private boolean isEnable;



    //true为锁定
    @TableField(value = "locked")
    private boolean isNoLocked;



    /**
     * 非数据库字段
     */

    @TableField(exist = false)
//    @JsonIgnore
    private List<Role> roles;

    @TableField(exist = false)
    private String menuUrls;



    @TableField(exist = false)
//    @JsonIgnore
    private Doctor doctor;



    /**
     * 自定义方法
     */
    public Account() {
    }

    public Account(String username, String password) {
        this.username = username;
        this.password = password;
        this.bcryptPassword();
    }

    public void bcryptPassword() {
        if (this.password == null || "".equals(this.password.trim())) {
            throw new NullPointerException("密码为空");
        }
        this.password = new BCryptPasswordEncoder().encode(this.password);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        Account account = (Account) o;
        return Objects.equals(username, account.username);
    }

    /**
     * 获取当前用户对象所具有的角色信息
     */
    @Override
    @JsonIgnore
    public Collection<? extends GrantedAuthority> getAuthorities() {
//        System.out.println("----------------> User.getAuthorities()");
        List<SimpleGrantedAuthority> authorities = new ArrayList<>();
        for (Role role : roles) {
            authorities.add(new SimpleGrantedAuthority(role.getName()));
        }
        return authorities;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }
    /**
     * 当前账户是否未锁定
     */
    @Override
    public boolean isAccountNonLocked() {
        return !isNoLocked;
    }
    /**
     * 当前账户密码是否未过期
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }
    /**
     * 当前账户是否可用
     */
    @Override
    public boolean isEnabled() {
        return isEnable;
    }
}